<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 工序数据 -->
      <el-col :span="3" :xs="24">
        <el-tree ref="tree"  :data="lineDate" :props="defaultProps" node-key="id" highlight-current @node-click="lineClick" />
      </el-col>
      <el-col :span="21" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="上游工序" prop="upstreamProcessId">
        <el-input
          v-model="queryParams.upstreamProcessId"
          placeholder="请输入上游工序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下游工序" prop="downstreamProcessId">
        <el-input
          v-model="queryParams.downstreamProcessId"
          placeholder="请输入下游工序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="机器转动信息" prop="turnInfo">
        <el-input
          v-model="queryParams.turnInfo"
          placeholder="请输入机器转动信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="动作类型" prop="actionType">
        <el-select v-model="queryParams.actionType" placeholder="请选择动作类型" clearable>
          <el-option v-for="dict in actionTypes" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zndd:processInterval:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zndd:processInterval:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zndd:processInterval:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zndd:processInterval:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processIntervalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
      <el-table-column label="上游工序" align="center" prop="upstreamProcessId" :formatter="upstreamProcessFormatter"/>
      <el-table-column label="下游工序" align="center" prop="downstreamProcessId" :formatter="downstreamProcessFormatter"/>
      <el-table-column label="机器转动信息" align="center" prop="turnInfo" />
      <el-table-column label="机械状态" align="center" prop="machineType" />
      <el-table-column label="动作类型" align="center" prop="actionType" :formatter="actionTypeFormatter"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zndd:processInterval:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:processInterval:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-col>
</el-row>
    <!-- 添加或修改工序间隔信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item> -->
        <!-- <el-form-item label="上游工序id" prop="upstreamProcessId">
          <el-input v-model="form.upstreamProcessId" placeholder="请输入上游工序id" />
        </el-form-item> -->
        <!-- <el-form-item label="下游工序id" prop="downstreamProcessId">
          <el-input v-model="form.downstreamProcessId" placeholder="请输入下游工序id" />
        </el-form-item> -->
        <el-form-item label="产线" prop="lineNo" v-if="this.lineList.length > 0 && this.currentLineNo == null">
          <el-select v-model="form.lineNo" placeholder="请选择产线" @change="selectProcess">
            <el-option v-for="item in lineList" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="上游工序" prop="upstreamProcessId">
          <el-select v-model="form.upstreamProcessId" placeholder="请选择动作类型">
            <el-option v-for="dict in selectProcessList" :key="dict.id" :label="dict.processName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="下游工序" prop="downstreamProcessId">
          <el-select v-model="form.downstreamProcessId" placeholder="请选择动作类型">
            <el-option v-for="dict in selectProcessList" :key="dict.id" :label="dict.processName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="机器转动信息" prop="turnInfo">
          <el-input v-model="form.turnInfo" placeholder="请输入机器转动信息" />
        </el-form-item>
        <el-form-item label="机械状态" prop="machineType">
          <el-input v-model="form.machineType" placeholder="请输入机械状态" />
        </el-form-item>
        <el-form-item label="动作类型" prop="actionType">
          <el-select v-model="form.actionType" placeholder="请选择动作类型">
            <el-option v-for="dict in actionTypes" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcessInterval, getProcessInterval, delProcessInterval, addProcessInterval, updateProcessInterval } from "@/api/zndd/process/processInterval";
import { listAllprocess } from "@/api/zndd/process/process";
import { listAllDetails } from "@/api/zndd/line/details";
import { listAllSet } from "@/api/zndd/line/set";
import { getDicts } from "@/api/system/dict/data";

export default {
  name: "ProcessInterval",
  data() {
    return {
      //当前产线数组
      lineList:[],
      //当前产线编号
      currentLineNo:null,
      //动作类型列表
      actionTypes: [],
      //筛选工序列表
      selectProcessList: [],
      //全部工序列表
      allProcessList: [],
       //二级产线数据
      lineDate: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      //二级产线全部数据
      detailsList:[],
      //三级产线全部数据
      lineSetList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序间隔信息表格数据
      processIntervalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        lineNo:null,
        upstreamProcessId: null,
        downstreamProcessId: null,
        turnInfo: null,
        machineType: null,
        actionType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //获取字典表中工序类型
    this.getDicts("action_type").then(response => {
      this.actionTypes = response.data;
    });
    listAllprocess().then(response => {
      this.allProcessList = response.rows;
    })
    this.getLineDetalisList();
  },
  methods: {
    //新增修改产线事件
    selectProcess(value){
      var ps = [];
        this.allProcessList.forEach(process => {
            if(process.lineNo == value){
              ps.push(process);
            }
          });
        this.selectProcessList=ps;
    },
    //格式化上游工序名称
    upstreamProcessFormatter(row, column) {
      var up = null;
      this.allProcessList.forEach(process => {
        if (process.id == row.upstreamProcessId) {
          up = process;
        }
      });
      if (up == null) {
        return "";
      } else {
        return up.processName;
      }
    },
    //格式化下游工序名称
    downstreamProcessFormatter(row, column) {
      var up = null;
      this.allProcessList.forEach(process => {
        if (process.id == row.downstreamProcessId) {
          up = process;
        }
      });
      if (up == null) {
        return "";
      } else {
        return up.processName;
      }
    },
    //格式化动作类型
    actionTypeFormatter(row, column) {
      var at = null;
      this.actionTypes.forEach(actionType => {
        if (actionType.dictValue == row.actionType) {
          at = actionType;
        }
      });
      if (at == null) {
        return "";
      } else {
        return at.dictLabel;
      }
    },
    //查询二级产线信息
    getLineDetalisList(){
      listAllDetails().then(response => {
        this.detailsList = response.rows;
        this.getLineSetList();
      });
    },
    //查询三级产线信息
    getLineSetList(){
      listAllSet().then(response => {
        var list = [];
        this.detailsList.forEach(details => {
          var sets = [];
          response.rows.forEach(row => {
            if(details.id == row.lineId){
              sets.push({
                label:row.threeLineName,
                value:row.lineNo,
                lineId:row.lineId,
              });
            }
          });
          list.push({
            id:details.id,
            label:details.sonLineName,
            value:details.lineNo,
            children:sets
          });
        });
        this.lineDate = list;
        this.$nextTick(()=>{
          this.$refs.tree.setCurrentKey(this.lineDate[0].id);
        })
        this.lineClick(this.lineDate[0]);
      });
    },
    //树型二级产线点击事件
    lineClick(data) {
      this.queryParams.lineNo = null;
      this.queryParams.lineNos = [];
      this.lineList = [];
      this.currentLineNo = null;
      this.selectProcessList= this.allProcessList;
      if(data.children != undefined && data.children.length > 0){
        var lns = [];
        var lines = [];
        data.children.forEach(row => {
          if(row.value != undefined){
              lns.push(row.value);
              lines.push(row);
            }
        });
        if(lns.length != 0){
          this.queryParams.lineNos = lns;
          this.lineList = lines;
        }
      }else{
        this.queryParams.lineNo = data.value;
        this.currentLineNo = data.value;
        var ps = [];
        this.allProcessList.forEach(process => {
            if(process.lineNo == data.value){
              ps.push(process);
            }
          });
        this.selectProcessList=ps;
      }
      if(this.queryParams.lineNo == null && this.queryParams.lineNos.length == 0){
        this.loading = false;
      }else{
        this.getList();
      }
    },
    /** 查询工序间隔信息列表 */
    getList() {
      this.loading = true;
      listProcessInterval(this.queryParams).then(response => {
        this.processIntervalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        lineNo:null,
        upstreamProcessId: null,
        downstreamProcessId: null,
        turnInfo: null,
        machineType: null,
        actionType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序间隔信息";
      this.form.lineNo = this.currentLineNo;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcessInterval(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工序间隔信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProcessInterval(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessInterval(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的工序间隔信息数据？').then(function() {
        return delProcessInterval(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/processInterval/export', {
        ...this.queryParams
      }, `工序间隔_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="css" scoped>
::v-deep .el-tree-node__content:hover{
  background-color: #409EFF;
  color: #ffffff;
}
::v-deep .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content{
  background-color: #409EFF;
  color: #ffffff;
}
</style>